<?php
declare (strict_types=1);

namespace app\middleware;

use app\common\Jwt;
use app\traits\ApiResponseTrait;
use think\helper\Arr;
use think\Response;

class ApiAuth
{
    use ApiResponseTrait;

    protected $appkey = 'AeyJpc3MiOiIiLCJhdWQiOiIiLCJpYXQ';

    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        if (!$appKey = getHeaderAppKey($request->header())) {
            return $this->error('缺少参数app-key');
        }

        if ($appKey != $this->appkey) {
            return $this->error('不合法的的app-key');
        }
//        if (!$token = getHeaderToken($request->header())) {
//            return $this->error('缺失登录凭证');
//        }
//        [$isOk, $result] = Jwt::parseUserInfo($token, 'api');
//        if (!$isOk) {
//            return $this->error($result, 403);
//        }
//        if ($result['expire'] < time()) {
//            return $this->error('登录状态已经过期', 401);
//        }
//        //设置用户信息
//        $request->user = Arr::get($result, 'user');
        return $next($request);
    }
}
